package com.pansoft.klinelib.model;

import com.pansoft.klinelib.util.DoubleUtil;

import java.util.ArrayList;
import java.util.List;

public class BOLL {
    /**
     * 不计算的前几个点，默认为0，也不在图标上显示出来
     */
    public static final int BOLL_NUM_NOT_DRAW = 19;
    /**
     * 上轨线
     */
    private ArrayList<Double> UPs;
    /**
     * 中轨线
     */
    private ArrayList<Double> MBs;
    /**
     * 下轨线
     */
    private ArrayList<Double> DNs;

    public BOLL(List<Double> ma20List, List<HisData> kLineBean) {

        UPs = new ArrayList<>();

        MBs = new ArrayList<>();

        DNs = new ArrayList<>();

        ArrayList<Double> uPs = new ArrayList<>();
        ArrayList<Double> mBs = new ArrayList<>();
        ArrayList<Double> dNs = new ArrayList<>();

        double mb = 0;
        double up = 0;
        double dn = 0;

        if (kLineBean != null && kLineBean.size() > 0) {
            for (int i = 0; i < kLineBean.size(); i++) {
                HisData point = kLineBean.get(i);
                if (i >= BOLL_NUM_NOT_DRAW) {
                    int n = 20;
                    double md = 0;
                    for (int j = i - n + 1; j <= i; j++) {
                        double c = kLineBean.get(j).getClose();
                        double m = ma20List.get(i);
                        double value = c - m;
                        md += value * value;
                    }
                    md = md / (n - 1);
                    md = (double) Math.sqrt(md);
                    mb = ma20List.get(i);
                    up = mb+ 2f * md;
                    dn = mb - 2f * md;
                }
                double upResult = Double.valueOf(DoubleUtil.assignNumberFormat(up, 8, 7));
                uPs.add(upResult);
                double mbResult = Double.valueOf(DoubleUtil.assignNumberFormat(mb, 8, 7));
                mBs.add(mbResult);
                double dnResult = Double.valueOf(DoubleUtil.assignNumberFormat(dn, 8, 7));
                dNs.add(dnResult);
            }

            for (int i = 0; i < uPs.size(); i++) {
                UPs.add(uPs.get(i));
                MBs.add(mBs.get(i));
                DNs.add(dNs.get(i));
            }
        }
    }

    public ArrayList<Double> getUPs() {
        return UPs;
    }

    public ArrayList<Double> getMBs() {
        return MBs;
    }

    public ArrayList<Double> getDNs() {
        return DNs;
    }
}
